package com.vixo.unityandroid;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.util.Log;

import com.vixo.videopager.VixoPlayerFragment;

//帮助类,用于在Unity中与Android原生代码交互。
//可以在Unity界面上打开任意Activity。
//也可以在Unity界面上加入一个Fragment层。

public class XYZActivityHelper {

    private static final boolean DEBUG=false;

    public static XYZActivityHelper Instance;

    private String urls ="";

    static Fragment testFragment;
    static Fragment anyFragment;

    //为不同程序间传递变量,showAnyActivity用。
    private static Class anyActivity;

    public static void showTest(final Activity launcher)
    {
        showActivity(launcher,TestActivity.class);
    }

    public static void showActivity(final Activity launcher, Class activityClass)
    {
        Intent intent = new Intent();
        intent.setClass(launcher, activityClass);
        launcher.startActivity(intent);
    }

    public static void setAnyActivity(Class cls)
    {
        anyActivity = cls;
    }

    public static void showAnyActivity(final Activity launcher)
    {
        showActivity(launcher,anyActivity);
    }

    public static void workListResult(final Activity launcher, String urls)
    {
        //this.urls = urls;
        Instance.OnReceivedWorkList(urls);
    }

    public static void showTestFrag(Activity launcher)
    {
        showFragment(launcher, getTestFragment());
    }

    public static void showVixoVideoPlayFrag(Activity launcher) {
        showFragment(launcher,new VixoPlayerFragment());
    }

    private static Fragment getTestFragment() {
        if(testFragment==null) {
            testFragment = new TestFragment();
        }
        return testFragment;
    }

    public static void showFragment(Activity launcher, Fragment frag)
    {

        FragmentTransaction ft = launcher.getFragmentManager().beginTransaction();
        ft.add(android.R.id.content, frag).commit();
    }

    public static void setAnyFragment(Fragment frag)
    {
        anyFragment = frag;
    }

    public static void showAnyFragment(Activity launcher)
    {
        showFragment(launcher,anyFragment);
    }


    public String getUrls()
    {
        return this.urls;
    }

    protected void OnReceivedWorkList(String urls)
    {

    }

    public void getUrlsFromUnity(String id) {

        if (DEBUG) {
            OnReceivedWorkList("20A66C8B0CD46A26037EDA2F64A99E5C.mp4,qjdh_clip.mp4,JIANGZHENDE_clip.mp4");
        }
        else
        {

            try {
                if (null != Class.forName("com.unity3d.player.UnityPlayer")) {
                 //   UnityPlayer.UnitySendMessage("ApiManager", "GetWorkList", id);
                }
            } catch (Exception ex) {
                Log.e("XYZAndroidUnity", ex.getClass().getName() + ":" + ex.getMessage());
            }
        }
    }
}
